/* 基于 README 的 window.bridge 适配封装 */

declare const window: any;

type ProxyOptions = {
  url: string;
  method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
  headers?: Record<string, string>;
  body?: any;
};

export function bridgeProxy<T = any>(options: ProxyOptions): Promise<T> {
  const { url, method = 'GET', headers, body } = options;
  
  return new Promise((resolve, reject) => {
    try {
      window.bridge.call(
        'open.api.proxy',
        { url, method, headers, body },
        (res: any) => {
          resolve(res);
        },
        (err: any) => {
          reject(err);
        }
      );
    } catch (error) {
      reject(error);
    }
  });
}

type OpenApiRequest = {
  version: string;
  namespace: string;
  name: string;
  data?: Record<string, any>;
};

export function bridgeOpenApi<T = any>(payload: OpenApiRequest): Promise<T> {
  return new Promise((resolve, reject) => {
    try {
      window.bridge.call('open.api.request', payload, (res: any) => resolve(res), (err: any) => reject(err));
    } catch (error) {
      reject(error);
    }
  });
}


